/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.alex.jbpm.basic;


import junit.framework.TestCase;

import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;

public class HelloWorldTest extends TestCase {
  
  public void testHelloWorldProcess() {
    // This method shows a process definition and one execution
    // of the process definition.  The process definition has 
    // 3 nodes: an unnamed start-state, a state 's' and an 
    // end-state named 'end'.
    // The next line parses a piece of xml text into a
    // ProcessDefinition.  A ProcessDefinition is the formal 
    // description of a process represented as a java object.
    ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state>" +
      "    <transition to='s' />" +
      "  </start-state>" +
      "  <state name='s'>" +
      "    <transition to='end' />" +
      "  </state>" +
      "  <end-state name='end' />" +
      "</process-definition>"
    );
    
    // The next line creates one execution of the process definition.
    // After construction, the process execution has one main path
    // of execution (=the root token) that is positioned in the
    // start-state.
    ProcessInstance processInstance = 
        new ProcessInstance(processDefinition);
    
    // After construction, the process execution has one main path
    // of execution (=the root token).
    Token token = processInstance.getRootToken();
    
    // Also after construction, the main path of execution is positioned
    // in the start-state of the process definition.
    assertSame(processDefinition.getStartState(), token.getNode());
    
    // Let's start the process execution, leaving the start-state 
    // over its default transition.
    token.signal();
    // The signal method will block until the process execution 
    // enters a wait state.

    // The process execution will have entered the first wait state
    // in state 's'. So the main path of execution is not 
    // positioned in state 's'
    assertSame(processDefinition.getNode("s"), token.getNode());

    // Let's send another signal.  This will resume execution by 
    // leaving the state 's' over its default transition.
    token.signal();
    // Now the signal method returned because the process instance 
    // has arrived in the end-state.
    
    assertSame(processDefinition.getNode("end"), token.getNode());
  }

  
  public void testThreeStateProcess() {
    // This test shows a process similar to the simplest process 
    // above, but now there are 3 states instead of one, which will 
    // result in 2 extra signals to be given by the test. 
    ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state>" +
      "    <transition to='phase one' />" +
      "  </start-state>" +
      "  <state name='phase one'>" +
      "    <transition to='phase two' />" +
      "  </state>" +
      "  <state name='phase two'>" +
      "    <transition to='phase three' />" +
      "  </state>" +
      "  <state name='phase three'>" +
      "    <transition to='end' />" +
      "  </state>" +
      "  <end-state name='end' />" +
      "</process-definition>"
    );
    
    ProcessInstance processInstance = 
        new ProcessInstance(processDefinition);
    Token token = processInstance.getRootToken();
    assertSame(processDefinition.getStartState(), token.getNode());

    token.signal();
    assertSame(processDefinition.getNode("phase one"), token.getNode());

    token.signal();
    assertSame(processDefinition.getNode("phase two"), token.getNode());

    token.signal();
    assertSame(processDefinition.getNode("phase three"), token.getNode());

    token.signal();
    assertSame(processDefinition.getNode("end"), token.getNode());
  }
}
